package com.itouchtv.yang.core.dataBase;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
@Deprecated
public class Select {
    private static Statement statement = null;
    private static ResultSet rs = null;

    public static List getMySqlData(String sql, String columnName) {
        statement = Connect.getStatement();
        List<Object> list = new ArrayList<>();
        try {
            list.clear();
            rs = statement.executeQuery(sql);
            while (rs.next()) {
                list.add(rs.getObject(columnName));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            close();
        }
        return list;
    }

    public static List getMySqlData(String sql){
        statement = Connect.getStatement();
        List<List> list = new ArrayList<>();
        try {
            list.clear();
            rs = statement.executeQuery(sql);
            int count = rs.getMetaData().getColumnCount();
            while (rs.next()) {
                List<Object> listRow = new ArrayList<>();
                for(int i=1;i<=count;i++){
                    listRow.add(rs.getObject(i));
                }
                list.add(listRow);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        close();
        return list;
    }

    private static void close(){
        if (rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (statement!=null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
